package com.sduept.nwld.dataserver.controller.equipment.assessment;

import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.equipment.assessment.model.hiddendanger.knowledgebase.KnowledgeBase;
import com.sduept.bigdata.equipment.assessment.model.hiddendanger.knowledgebase.KnowledgeQueryParamModel;
import com.sduept.bigdata.equipment.assessment.model.standarddict.HiddenDangerLevel;
import com.sduept.bigdata.equipment.assessment.model.standarddict.HiddenDangerQuesType;
import com.sduept.bigdata.equipment.assessment.model.standarddict.HiddenDangerReasonType;
import com.sduept.bigdata.equipment.assessment.model.standarddict.HiddenDangerSource;
import com.sduept.bigdata.equipment.assessment.service.HiddenDangerKnowledageBaseService;
import com.sduept.bigdata.equipment.assessment.service.InspectionExpertService;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.core.entity.AbstractStandardDict;
import com.sduept.core.service.StandardDictService;
import org.primefaces.PrimeFaces;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识库选择器
 * 
 *@author xuqinwen
 */
@Named
@ViewScoped
public class KnowledgeBaseSelectorController extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7476690330151852136L;
	@Autowired
	private HiddenDangerKnowledageBaseService hdkbService;
	@Autowired
	private InspectionExpertService expertService ;	
    @Autowired
    private StandardDictService dictService;
	
	
	private List<String> quesList ; // 问题归类
	private List<String> reasonList ;//原因归类
	private List<String> gradeList ; // 隐患等级
    private List<String> sourceList; // 隐患来源
    
	private KnowledgeQueryParamModel queryParam; 	
	private List<KnowledgeBase> kbs ;
	private KnowledgeBase selectKb = null;
    private String selectedQuesDesc =  "{}";//问题描述
    private String modelId;
    
	@PostConstruct
	private void init() {
		try {
			 setModelId(getParameter("modelId"));
			kbs = hdkbService.findAllKnowledgeBases();
			quesList = getStringList(HiddenDangerQuesType.class);
			gradeList = getStringList(HiddenDangerLevel.class);
			reasonList = getStringList(HiddenDangerReasonType.class);
			sourceList=getStringList(HiddenDangerSource.class);
			queryParam=new KnowledgeQueryParamModel();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	private <T> List<String> getStringList(Class<T> T) throws Exception {
		List<String> results = new ArrayList<String>();
		List<AbstractStandardDict> list = (List<AbstractStandardDict>) dictService.findAll(Class.forName(T.getName()));
		for (AbstractStandardDict s : list) {
			results.add(s.getName());
		}
		return results;
	}
	
	/**
	 * 页面查询方法
	 */
	public void query() {		
		try {
			kbs=hdkbService.findByCondition(queryParam.getCondition());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	  public void resetQuery() {	
    	init();
		selectKb = null;
	  }


	public void selectStandards(){
		List<KnowledgeBase> selects = new ArrayList<KnowledgeBase>();
		KnowledgeBase base = new KnowledgeBase();
		if(selectKb!=null) {
			base = selectKb;
			selects.add(selectKb);
		}
		
		//PrimeFaces.current().dialog().closeDynamic(base);
		PrimeFaces.current().dialog().closeDynamic(selects);
		
	}
	public void setQuesDesc() {
		if(selectKb!=null && !modelId.isEmpty()) {
		String data =selectKb.getHiddenDangerDesc();
		JSONObject r = new JSONObject();
		r.put("data", data);
		r.put("modelId",modelId);
		selectedQuesDesc = r.toJSONString();
		}
	}

	public void RowSelect(SelectEvent event) {
		selectKb = (KnowledgeBase) event.getObject();
	}

	public List<KnowledgeBase> getKbs() {
		return kbs;
	}
	public void setKbs(List<KnowledgeBase> kbs) {
		this.kbs = kbs;
	}

	public KnowledgeBase getSelectKb() {
		return selectKb;
	}

	public void setSelectKb(KnowledgeBase selectKb) {
		this.selectKb = selectKb;
	}
	public List<String> getQuesList() {
		return quesList;
	}
	public void setQuesList(List<String> quesList) {
		this.quesList = quesList;
	}
	public List<String> getReasonList() {
		return reasonList;
	}
	public void setReasonList(List<String> reasonList) {
		this.reasonList = reasonList;
	}
	public List<String> getGradeList() {
		return gradeList;
	}
	public void setGradeList(List<String> gradeList) {
		this.gradeList = gradeList;
	}
	public List<String> getSourceList() {
		return sourceList;
	}
	public void setSourceList(List<String> sourceList) {
		this.sourceList = sourceList;
	}
	public KnowledgeQueryParamModel getQueryParam() {
		return queryParam;
	}
	public void setQueryParam(KnowledgeQueryParamModel queryParam) {
		this.queryParam = queryParam;
	}

	public String getSelectedQuesDesc() {
		return selectedQuesDesc;
	}

	public void setSelectedQuesDesc(String selectedQuesDesc) {
		this.selectedQuesDesc = selectedQuesDesc;
	}

	public String getModelId() {
		return modelId;
	}

	public void setModelId(String modelId) {
		this.modelId = modelId;
	}

}
